<template>
  <div class="cmd-root" @click="focusInput">
    <div class="cmd-window">
      <div v-for="(line, idx) in output" :key="idx" class="cmd-line">{{ line }}</div>
      <div class="cmd-input-line">
        <span class="cmd-prompt">C:\&gt;</span>
        <input ref="inputRef" v-model="input" @keydown.enter="onEnter" class="cmd-input" autocomplete="off" />
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, nextTick } from 'vue'
const output = ref([
  'Microsoft Windows [版本 10.0.22631.1]',
  '(c) Microsoft Corporation。保留所有权利。',
  ''
])
const input = ref('')
const inputRef = ref(null)
const safeCommands = ['dir', 'ipconfig', 'echo', 'help']

function focusInput() {
  inputRef.value && inputRef.value.focus()
}

async function onEnter() {
  const cmd = input.value.trim()
  const cmdName = cmd.split(' ')[0].toLowerCase()
  output.value.push('C:> ' + cmd)
  if (!safeCommands.includes(cmdName)) {
    output.value.push('不允许执行该命令')
    input.value = ''
    nextTick(focusInput)
    return
  }
  if (window.electronAPI && window.electronAPI.runCmd) {
    const result = await window.electronAPI.runCmd(cmd)
    output.value.push(result)
  } else {
    output.value.push('（仿真模式）')
  }
  input.value = ''
  nextTick(focusInput)
}

function handleCmd(cmd) {
  if (cmd === 'help') {
    output.value.push('可用命令: help, dir, echo, cls, exit')
  } else if (cmd === 'dir') {
    output.value.push(' 文件夹: .', ' 文件夹: ..', ' 文件: README.md', ' 文件: hello.txt')
  } else if (cmd.startsWith('echo ')) {
    output.value.push(cmd.slice(5))
  } else if (cmd === 'cls') {
    output.value = []
  } else if (cmd === 'exit') {
    output.value.push('窗口已关闭（模拟）')
  } else {
    output.value.push('不是内部或外部命令，也不是可运行的程序')
  }
}
</script>
<style scoped>
.cmd-root { background: #111; color: #0f0; border-radius: 8px; box-shadow: 0 4px 24px #0008; padding: 16px; min-width: 480px; min-height: 320px; font-family: "Consolas", "Courier New", monospace; }
.cmd-window { min-height: 280px; }
.cmd-line { white-space: pre-wrap; line-height: 1.6; }
.cmd-input-line { display: flex; align-items: center; margin-top: 2px; }
.cmd-prompt { color: #0f0; margin-right: 4px; }
.cmd-input { background: transparent; border: none; outline: none; color: #0f0; font-family: inherit; font-size: 1em; width: 80%; }
</style> 